#!/bin/ksh -e

# This file and its contents are supplied under the terms of the
# Common Development and Distribution License ("CDDL"), version 1.0.
# You may only use this file in accordance with the terms of version
# 1.0 of the CDDL.
#
# A full copy of the text of the CDDL should have accompanied this
# source. A copy of the CDDL is also available via the Internet at
# http://www.illumos.org/license/CDDL.

# Copyright 2017 OmniOS Community Edition (OmniOSce) Association.

# Setup utility for creating an OmniOS build environment.

mydir=`dirname "$_"`

ips=https://pkg.omniosce.org
github=https://github.com
role=1

export LC_ALL=C

# Determine the running OmniOS release.
version=`uname -v`

if [[ "$version" != omnios-* ]]; then
	echo "This does not appear to be an OmniOS system."
	exit 1
fi

release=`echo "$version" | cut -d- -f2`
branch=$release
if [ "$release" = master ]; then
	release=bloody
	branch=master
	relnum="`head -1 /etc/release | awk '{print $3}' | cut -c2-`"
else
	relnum="${release:1}"
fi

ask()
{
	typeset a=

	echo "$* (y/n) \\c"
	while [[ "$a" != [yYnN] ]]; do
		read -r a
	done
	[[ "$a" = [yY] ]]
}

determine_role()
{
	role=1
	cat << EOM

-- If you intend to contribute to OmniOS development, then additional
-- commands can be enabled to assist with the process.

EOM

	ask "--- Enable these additional commands" && role=2
	echo

	if [ "$role" -eq 2 ]; then
		cat << EOM

-- Additionally, if you have write access to the omniosorg organisation at
-- GitHub, then the remote upstream branches can be automatically kept
-- up-to-date. This access is NOT necessary in order to develop since
-- all integrations are via pull requests.

EOM
		ask "--- Do you have commit access to github/omniosorg?" \
		    && role=3
		echo
	fi
}

install_pkg()
{
	sudo pkg install "$1"
}

check_pkg()
{
	typeset pkg="$1"

	echo "--- checking that $pkg is installed"
	if ! pkg list -q "$pkg"; then
		echo
		echo "The $pkg package is not installed on this system."
		if ask "Install?"; then
			echo
			install_pkg "$pkg"
		else
			echo "Aborting setup."
			exit 1
		fi
	fi

}

check_publisher()
{
	echo "--- checking that extra.omnios publisher is set"
	if ! pkg publisher -H | egrep -s '^extra.omnios '; then
		echo
		echo "The extra.omnios publisher is not present."
		if ask "Add it?"; then
			echo
			sudo pkg set-publisher \
			    -g $ips/$release/extra extra.omnios || exit 1

			pkg publisher
		else
			echo "Aborting setup."
			exit 1
		fi
	fi
}

gitclone()
{
	typeset repo=$1
	typeset user=${2:-$gituser}

	echo "--- Cloning $repo from ${github}/$user"
	[ -d "$wd/$repo/.git" ] && return
	git clone -b "$branch" "${github}/$user/$repo.git" "$wd/$repo"

	(
		cd "$wd/$repo"
		git remote add upstream "${github}/omniosorg/$repo"
		if [ "$repo:$branch" = "illumos-omnios:master" ]; then
			git remote add upstream_gate \
			    "${github}/illumos/illumos-gate"
			git remote add upstream_joyent \
			    "${github}/joyent/illumos-joyent"
		fi
	)
}

install_config()
{
	echo "--- Installing configuration file"
	cat << EOM > "$HOME/.omni"
github=${github}
gituser=$gituser
release=$release
branch=$branch
wd=$wd
# 1 user, 2 developer, 3 guardian (commit access)
role=$role
EOM
}

install_env()
{
	echo "--- Install omnios.env"
	if [ $relnum -lt 151031 ]; then
		template="template-gcc4.env"
	else
		template="template-gcc7.env"
	fi
	sed "
		s^__WORKINGTREE__^$wd^
		s^__BUILDNUM__^$relnum^
	" < "$mydir/etc/$template" > "$wd/omnios.env"
	# Translate the GCC root variable back when building old versions
	if [ $relnum -lt 151027 ]; then
		sed -i 's/GNUC_ROOT/GCC_ROOT/g' "$wd/omnios.env"
	fi
	# Enable the shadow compiler from r151029
	if [ $relnum -ge 151029 ]; then
		sed -i '/CW_NO_SHADOW/d' "$wd/omnios.env"
	fi
}

install_site()
{
	echo "--- Install site.sh"
	cat << EOM >> "$wd/omnios-build/lib/site.sh"

# Settings added by omni setup

PREBUILT_ILLUMOS=$wd/illumos-omnios
PKGSRVR=file://$wd/repo
TMPDIR=$wd/tmp
DTMPDIR=\$TMPDIR
if [ \`id -u\` = '0' ]; then
	ROOT_OK=1
	export FORCE_UNSAFE_CONFIGURE=1
fi
#SKIP_KAYAK_KERNEL=1

EOM

}

install_hooks()
{
	echo "--- Install hooks"
	cp "$mydir/etc/prepare-commit-msg"* "$wd/illumos-omnios/.git/hooks/"
	chmod 755 "$wd/illumos-omnios/.git/hooks/prepare-commit-msg"
}

echo "This system is running OmniOS $release"

if [ "$1" = "-ssh" ]; then
	github=git@github.com:
	echo "Using SSH for GitHub access"
	shift
fi

wd="$1"
gituser="$2"
[ -z "$gituser" ] && gituser=omniosorg

if [ -z "$wd" ]; then
	echo "Syntax: setup [-ssh] <working directory> [git username]"
	exit 1
fi

if [[ "$gituser" != "omniosorg" && "$github" != git@* ]]; then
	cat << EOM

-- By default, HTTPS will be used to access GitHub.
-- If you have configured SSH keys on GitHub you can use SSH instead.

EOM
	if ask "Do you want to use SSH instead?"; then
		github=git@github.com:
	fi
	echo
fi

if [ -d "$wd" ]; then
	if [ "`find "$wd" | wc -l`" -ne 1 ]; then
		echo "$wd is not empty. Aborting setup."
		exit 1
	fi
fi

if ! mkdir -p "$wd"; then
	echo "Could not create $wd directory."
	exit 1
else
	echo "--- Using $wd for installation."
fi

[ "$gituser" != omniosorg ] && determine_role
check_publisher
check_pkg developer/illumos-tools
check_pkg developer/omnios-build-tools
check_pkg ooce/omnios-build-tools

gitclone illumos-omnios
gitclone omnios-build
[ $role -gt 1 -a "$branch" = "master" ] && gitclone lx-port-data omniosorg
gitclone kayak

install_config
install_env
install_site
install_hooks

echo

if [[ $mydir = */ooce/* ]]; then
	cat << EOM

Setup is complete.
Invoke the utility by typing '/opt/ooce/bin/omni'
or add /opt/ooce/bin to your \$PATH

EOM
elif [ "`id -u`" = 0 ] && ask "Create symlink as /usr/bin/omni?"; then
	[ -h /usr/bin/omni ] && rm -f /usr/bin/omni
	ln -s "`pwd`/bin/omni" /usr/bin/omni
	echo
	echo "Invoke the utility by typing 'omni'"
else
	echo "Invoke the utility by typing '`pwd`/bin/omni'"
fi

